package avl;

//平衡二叉树
public class AVLTreeDemo {
    public static void main(String[] args) {
        int[] arr = {4, 3, 6, 5, 7, 8};
        int[] arr2 = {10, 12, 8, 9, 7, 6};
        AVLTree avlTree = new AVLTree();
        AVLTree avlTree2 = new AVLTree();
        for (int a :
                arr) {
            avlTree.add(new Node(a));
        }
        for (int a :
                arr2) {
            avlTree2.add(new Node(a));
        }
        avlTree.infixOrder();
        System.out.println(avlTree.getRoot().height());
        System.out.println(avlTree.getRoot().leftHeight());
        System.out.println(avlTree.getRoot().rightHeight());
        System.out.println("右旋转");
        avlTree2.infixOrder();
        System.out.println(avlTree2.getRoot().height());
        System.out.println(avlTree2.getRoot().leftHeight());
        System.out.println(avlTree2.getRoot().rightHeight());
        System.out.println(avlTree2.getRoot());
        System.out.println(avlTree2.getRoot().left);
        System.out.println(avlTree2.getRoot().right);
    }
}

//创建avl树
class AVLTree {


    private Node root;

    public Node getRoot() {
        return root;
    }

    public void add(Node node) {
        if (root == null) {
            root = node;
        } else {
            root.add(node);
        }

    }

    public Node search(int value) {
        if (root == null) {
            return null;
        } else {
            return root.search(value);
        }
    }

    public Node searchParent(int value) {
        if (root == null) {
            return null;
        } else {
            return root.searchParent(value);
        }
    }

    //删除节点
    public void delNode(int value) {
        if (root == null) {
            return;
        } else {
            Node targetNode = search(value);
            if (targetNode == null) {
                System.out.println("没有找到要删除的节点");
                return;
            }
            //如果没有发现targetNode没有父节点 相当于只有一个节点
            if (root.left == null && root.right == null) {
                root = null;
                return;
            }
            //有父节点的情况
            Node parent = searchParent(value);
            //如果要删除的是叶子节点的情况下
            if (targetNode.left == null && targetNode.right == null) { //叶子节点

                if (parent.left != null && parent.left.value == value) {//左子节点
                    parent.left = null;
                } else if (parent.right != null && parent.right.value == value) {//右子节点
                    parent.right = null;
                }
            } else if (targetNode.left != null && targetNode.right != null) {// 左右子树都有
                int minVal = delRightTreeMin(targetNode.right);
                targetNode.value = minVal;
            } else { //只有一颗子树的节点
                if (parent != null) {
                    if (targetNode.left != null) { //要删除的节点 有左节点
                        if (parent.left.value == value) {
                            parent.left = targetNode.left;
                        } else {  //删掉的节点是右边
                            parent.right = targetNode.left;
                        }
                    } else {
                        root = targetNode.left;
                    }
                } else { //要删除的节点 有右节点
                    if (parent != null) {
                        if (parent.left.value == value) {
                            parent.left = targetNode.right;
                        } else {
                            parent.right = targetNode.right;
                        }
                    } else {
                        root = targetNode.right;
                    }
                }
            }

        }
    }

    //返回以node为节点 的二叉排序树的最小值 删除以node为根节点的二叉排序树的最小节点
    public int delRightTreeMin(Node node) {
        Node temp = node;
        while (temp.left != null) {
            temp = temp.left;
        }
        delNode(temp.value);
        return temp.value;

    }

    public void infixOrder() {
        if (root == null) {
            System.out.println("为空不能遍历");
        } else {
            root.infixOrder();
        }
    }
}

class Node {
    int value;
    Node left;
    Node right;

    public Node(int value) {
        this.value = value;
    }

    //返回当前节点的高度
    public int height() {
        return Math.max(left == null ? 0 : left.height(), right == null ? 0 : right.height()) + 1;
    }

    //返回左子树高度
    public int leftHeight() {
        if (left == null) {
            return 0;
        }
        return left.height();
    }

    //返回右子树高度
    public int rightHeight() {
        if (right == null) {
            return 0;
        }
        return right.height();
    }

    //添加节点的方法 递归 按照二叉排序树的方法添加
    public void add(Node node) {
        if (node == null) {
            return;
        }
        //判断传入的值 和当前子树的值的关系
        if (node.value < this.value) {
            if (this.left == null) {
                this.left = node;
            } else {
                //递归向左子树添加
                this.left.add(node);
            }
        } else { //大于或者等于放在右边
            if (this.right == null) {
                this.right = node;
            } else {
                //递归向右子树添加
                this.right.add(node);
            }
        }
        //当添加一个节点后 如果 左右子树差值>1
        if (rightHeight() - leftHeight() > 1) {
            if (right != null && right.leftHeight() > right.rightHeight()) {
                right.rightRotate();
                leftRotate();
            } else {
                leftRotate();
            }
            return;
        }

        if (leftHeight() - rightHeight() > 1) {
            //当前节点的左子树的右节点>左节点
            if (left != null && left.rightHeight() > left.leftHeight()) {
                //先左子树进行左旋转
                left.leftRotate();
                //再对当前节点进行右旋转
                rightRotate();
            } else {
                rightRotate();
            }
        }
    }

    //查找要删除的节点
    public Node search(int value) {
        if (value == this.value) {
            return this;
        } else if (value < this.value) { //小于当前节点 应该向左子树递归查找
            if (this.left == null) {
                return null;
            }
            return this.left.search(value);
        } else {
            if (this.right == null) {
                return null;
            }
            return this.right.search(value);
        }
    }

    //查找要删除的节点的父节点 返回的是要删除节点的父节点
    public Node searchParent(int value) {
        if ((this.left != null && this.left.value == value) || (this.right != null && this.right.value == value)) {
            return this;
        } else {
            if (value < this.value && this.left != null) {
                return this.left.searchParent(value);
            } else if (value >= this.value && this.right != null) {
                return this.right.searchParent(value);
            } else { //没有找到父节点
                return null;
            }

        }
    }

    @Override
    public String toString() {
        return "Node{" + "value=" + value + '}';
    }

    //中序遍历
    public void infixOrder() {
        if (this.left != null) {
            this.left.infixOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.infixOrder();
        }
    }

    //左旋转
    private void leftRotate() {
        //创建一个新的节点
        Node newNode = new Node(value);
        //把新的节点的左子树设置成当前节点的左子树
        newNode.left = this.left;
        //把新的节点的右子树设置成当前节点的右子树的左子树
        newNode.right = this.right.left;
        //把当前节点的值换成右子节点的值
        this.value = right.value;
        //把当前节点的右子树设置成右子树的右子树
        this.right = this.right.right;
        //把当前节点的左子节点设置成新的节点
        this.left = newNode;
    }

    //右旋转
    private void rightRotate() {
        //创建一个新的节点
        Node newNode = new Node(value);

        newNode.right = this.right;

        newNode.left = this.left.right;

        this.value = left.value;

        this.left = this.left.left;

        this.right = newNode;
    }
}